package com.xzssmk.leisure.x.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.StaffMerchantMapper;
import com.xzssmk.leisure.x.po.SelectModel;
import com.xzssmk.leisure.x.po.StaffMerchant;
import com.xzssmk.leisure.x.service.BaseService;
import com.xzssmk.leisure.x.service.StaffMerchantService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;
import com.xzssmk.leisure.x.vo.StaffMerchantVo;

@Service
public class StaffMerchantServiceImpl extends BaseService implements StaffMerchantService {

    @Autowired
    private StaffMerchantMapper staffMerchantMapper;

    @Override
    public Map<String, Object> insert(StaffMerchantVo staffMerchantVo) {

        staffMerchantVo.setCreateTime(new Date());
        staffMerchantVo.setModifyTime(new Date());
        staffMerchantVo.setInviteTimes(new Byte("0"));
        staffMerchantVo.setDisable(new Byte("1"));
        if (!StringUtils.isEmpty(staffMerchantVo.getScenicSpot())) {
            String scenicSpot = staffMerchantVo.getScenicSpot();
            String[] split = scenicSpot.split("&");
            staffMerchantVo.setScenicSpotId(Integer.parseInt(split[0]));
            staffMerchantVo.setScenicSpot(split[1]);
        }
        if (!StringUtils.isEmpty(staffMerchantVo.getStoreLargess())) {
            String storeLargess = staffMerchantVo.getStoreLargess();
            String[] split = storeLargess.split("&");
            staffMerchantVo.setStoreLargessId(Integer.parseInt(split[0]));
            staffMerchantVo.setStoreLargess(split[1]);
        }

        Map<String, Object> map = new HashMap<>();
        Integer id = staffMerchantMapper.insert(staffMerchantVo);
        if (id > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(List<Integer> idlist) {
        Map<String, Object> map = new HashMap<>();
        if (staffMerchantMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> update(StaffMerchantVo staffMerchantVo) {
        staffMerchantVo.setModifyTime(new Date());
        if (!StringUtils.isEmpty(staffMerchantVo.getScenicSpot())) {
            String scenicSpot = staffMerchantVo.getScenicSpot();
            String[] split = scenicSpot.split("&");
            staffMerchantVo.setScenicSpotId(Integer.parseInt(split[0]));
            staffMerchantVo.setScenicSpot(split[1]);
        }
        if (!StringUtils.isEmpty(staffMerchantVo.getStoreLargess())) {
            String storeLargess = staffMerchantVo.getStoreLargess();
            String[] split = storeLargess.split("&");
            staffMerchantVo.setStoreLargessId(Integer.parseInt(split[0]));
            staffMerchantVo.setStoreLargess(split[1]);
        }
        Map<String, Object> map = new HashMap<>();
        if (staffMerchantMapper.updateByPrimaryKeySelective(staffMerchantVo) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        // PageHelper.orderBy("modify_time desc");
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(StaffMerchantMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<StaffMerchantVo> staffMerchants = staffMerchantMapper.getPageList(dataTables.getSubSQL());
        PageInfo<StaffMerchantVo> pageInfo = new PageInfo<StaffMerchantVo>(staffMerchants);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public StaffMerchant selectOne(Integer id) {
        return staffMerchantMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SelectModel> getScenicSpotSelect() {
        return staffMerchantMapper.getScenicSpotSelect();
    }

    @Override
    public List<SelectModel> getStoreLargessSelect() {
        return staffMerchantMapper.getStoreLargessSelect();
    }
}
